#include "CIrrDeviceStub.h"
#include "IEventReceiver.h"
#include "irrList.h"
#include "os.h"
#include "IrrCompileConfig.h"
#include "CTimer.h"
#include "CLogger.h"
#include "Irrlicht.h"
#include "irrString.h"
#include <string.h> 
#include "CNetworkDriver.h"

namespace irr
{

	//! constructor
	CIrrDeviceStub::CIrrDeviceStub(const char* version, irr::IEventReceiver* resv)
		: Logger(0), Operator(0), VideoDriver(0)
	{
		UserReceiver = resv;

		// create timer
		Timer = new irr::CTimer();

        NetworkDriver =new game::net::CNetworkDriver();

		// create filesystem
		FileSystem = io::createFileSystem();

		Logger = new CLogger(UserReceiver, FileSystem);
		os::Printer::Logger = Logger;

		//core::stringw s = L"Irrlicht Engine version ";
		//s.append(getVersion());
		//os::Printer::log(s.c_str(), ELL_NONE);

		checkVersion(version);
	}


	CIrrDeviceStub::~CIrrDeviceStub()
	{
		FileSystem->drop();

		if (GUIEnvironment)
			GUIEnvironment->drop();

		if (VideoDriver)
			VideoDriver->drop();

		if (SceneManager)
			SceneManager->drop();

		if (CursorControl)
			CursorControl->drop();

		if (Operator)
			Operator->drop();
#ifdef _IRR_PSP_
		if (AudioDriver)
			AudioDriver->drop();
#endif
		if (NetworkDriver)
			NetworkDriver->drop();

		CursorControl = 0;

		Timer->drop();

		Logger->drop();
	}

      game::net::INetworkCallback* CIrrDeviceStub::createNetCallback()
        {
            return new  game::net::CNetworkCallback();
        }

	void CIrrDeviceStub::createGUIAndScene()
	{
		// create gui environment
		GUIEnvironment = gui::createGUIEnvironment(FileSystem, VideoDriver, Operator);

		// create Scene manager
		SceneManager = scene::createSceneManager(VideoDriver, FileSystem, CursorControl);

		setEventReceiver(UserReceiver);
	}


	//! returns the video driver
	video::IVideoDriver* CIrrDeviceStub::getVideoDriver()
	{
		return VideoDriver;
	}
#ifdef _IRR_PSP_
	audio::IAudioDriver* CIrrDeviceStub::getAudioDriver()
	{
		return AudioDriver;
	}
 #endif
	game::net::INetworkDriver* CIrrDeviceStub::getNetworkDriver()
	{
		return NetworkDriver;
	}


	//! return file system
	io::IFileSystem* CIrrDeviceStub::getFileSystem()
	{
		return FileSystem;
	}



	//! returns the gui environment
	gui::IGUIEnvironment* CIrrDeviceStub::getGUIEnvironment()
	{
		return GUIEnvironment;
	}



	//! returns the scene manager
	scene::ISceneManager* CIrrDeviceStub::getSceneManager()
	{
		return SceneManager;
	}


	//! \return Returns a pointer to the ITimer object. With it the
	//! current Time can be received.
	ITimer* CIrrDeviceStub::getTimer()
	{
		return Timer;
	}


	//! Returns the version of the engine.
	const char* CIrrDeviceStub::getVersion()
	{
		return IRRLICHT_SDK_VERSION;
	}

	//! \return Returns a pointer to the mouse cursor control interface.
	gui::ICursorControl* CIrrDeviceStub::getCursorControl()
	{
		return CursorControl;
	}


	//! \return Returns a pointer to a list with all video modes supported
	//! by the gfx adapter.
	video::IVideoModeList* CIrrDeviceStub::getVideoModeList()
	{
		return &VideoModeList;
	}


	//! checks version of sdk and prints warning if there might be a problem
	bool CIrrDeviceStub::checkVersion(const char* version)
	{
		if (strcmp(getVersion(), version))
		{
			core::stringw w;
			w = L"Warning: The library version of the Irrlicht Engine (";
			w += getVersion();
			w += L") does not match the version the application was compiled with (";
			w += version;
			w += L"). This may cause problems.";
			os::Printer::log(w.c_str(), ELL_WARNING);
			return false;
		}

		return true;
	}


	//! send the event to the right receiver
	void CIrrDeviceStub::postEventFromUser(SEvent event)
	{
		bool absorbed = false;

		if(	UserReceiver)
			absorbed = UserReceiver->OnEvent(event);

		if (!absorbed && GUIEnvironment)
			absorbed = GUIEnvironment->postEventFromUser(event);

		if (!absorbed && SceneManager)
			absorbed = SceneManager->postEventFromUser(event);
	}


	//! Sets a new event receiver to receive events
	void CIrrDeviceStub::setEventReceiver(IEventReceiver* receiver)
	{
		UserReceiver = receiver;
		Logger->setReceiver(receiver);
		if (GUIEnvironment)
			GUIEnvironment->setUserEventReceiver(receiver);
	}


	//! Returns poinhter to the current event receiver. Returns 0 if there is none.
	IEventReceiver* CIrrDeviceStub::getEventReceiver()
	{
		return UserReceiver;
	}


	//! \return Returns a pointer to the logger.
	ILogger* CIrrDeviceStub::getLogger()
	{
		return Logger;
	}


	//! Returns the operation system opertator object.
	IOSOperator* CIrrDeviceStub::getOSOperator()
	{
		return Operator;
	}


	//! Sets if the window should be resizeable in windowed mode.
	void CIrrDeviceStub::setResizeAble(bool resize)
	{

	}


} // end namespace irr
